/**
 * 接种点查询页面JavaScript逻辑
 */

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 检查登录状态
    if (!Auth.isLoggedIn()) {
        window.location.href = '../login.html';
        return;
    }

    // 检查用户角色
    if (!Auth.checkRole(UserRole.NORMAL_USER)) {
        UI.showMessage('您没有权限访问此页面', 'danger');
        setTimeout(() => {
            Auth.logout();
        }, 2000);
        return;
    }

    // 初始化页面
    init();
});

/**
 * 初始化页面
 */
async function init() {
    // 显示用户信息
    displayUserInfo();
    
    // 加载接种点列表
    await loadSiteList();
}

/**
 * 显示用户信息
 */
function displayUserInfo() {
    const userInfo = Auth.getUserInfo();
    if (userInfo) {
        const userName = userInfo.account || '用户';
        document.getElementById('userName').textContent = userName;
        
        // 设置头像首字母
        const avatar = document.getElementById('userAvatar');
        avatar.textContent = userName.charAt(0).toUpperCase();
    }
}

/**
 * 加载接种点列表
 */
async function loadSiteList() {
    try {
        // 获取筛选条件
        const nameFilter = document.getElementById('siteNameFilter').value.trim();
        const addressFilter = document.getElementById('siteAddressFilter').value.trim();
        
        // 获取接种点列表
        let sites = await HttpClient.get('/vaccinationsite/list');
        
        // 客户端过滤
        if (nameFilter) {
            sites = sites.filter(site => 
                site.name && site.name.toLowerCase().includes(nameFilter.toLowerCase())
            );
        }
        
        if (addressFilter) {
            sites = sites.filter(site => 
                site.address && site.address.toLowerCase().includes(addressFilter.toLowerCase())
            );
        }
        
        // 显示接种点列表
        displaySites(sites);
    } catch (error) {
        console.error('加载接种点列表失败:', error);
        UI.showMessage('加载接种点列表失败', 'danger');
    }
}

/**
 * 显示接种点列表
 */
function displaySites(sites) {
    const grid = document.getElementById('siteGrid');
    grid.innerHTML = '';
    
    if (!sites || sites.length === 0) {
        grid.innerHTML = `
            <div class="empty-state" style="grid-column: 1 / -1;">
                <i class="fas fa-hospital"></i>
                <p>暂无接种点信息</p>
            </div>
        `;
        return;
    }
    
    sites.forEach(site => {
        const card = createSiteCard(site);
        grid.appendChild(card);
    });
}

/**
 * 创建接种点卡片
 */
function createSiteCard(site) {
    const card = document.createElement('div');
    card.className = 'site-card';
    
    card.innerHTML = `
        <div class="site-card-header">
            <div class="site-icon">
                <i class="fas fa-hospital"></i>
            </div>
            <div class="site-title">
                <h4>${site.name || '未知接种点'}</h4>
                <p>
                    <span class="badge badge-success">营业中</span>
                </p>
            </div>
        </div>
        <div class="site-info">
            <div class="site-info-item">
                <i class="fas fa-map-marker-alt site-info-icon"></i>
                <div class="site-info-content">
                    <div class="site-info-label">地址</div>
                    <div class="site-info-value">${site.address || '-'}</div>
                </div>
            </div>
            <div class="site-info-item">
                <i class="fas fa-phone site-info-icon"></i>
                <div class="site-info-content">
                    <div class="site-info-label">联系电话</div>
                    <div class="site-info-value">${site.contactPhone || '-'}</div>
                </div>
            </div>
            <div class="site-info-item">
                <i class="fas fa-clock site-info-icon"></i>
                <div class="site-info-content">
                    <div class="site-info-label">营业时间</div>
                    <div class="site-info-value">${site.operatingHours || '周一至周日 8:00-18:00'}</div>
                </div>
            </div>
        </div>
        <div class="site-actions">
            <button class="btn btn-primary btn-sm" style="width: 100%;" onclick="goToAppointment()">
                <i class="fas fa-calendar-plus"></i> 前往预约
            </button>
        </div>
    `;
    
    return card;
}

/**
 * 跳转到预约页面
 */
function goToAppointment() {
    window.location.href = 'appointment.html';
}

/**
 * 退出登录
 */
function handleLogout() {
    UI.confirm('确定要退出登录吗？', () => {
        Auth.logout();
    });
}

